﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System;
using System.IO.Ports;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceProcess;
using System.Configuration;
namespace AISmartEventServer
{
    public partial class AISmartEventService : ServiceBase
    {

        AlarmMsgManager AlarmManager = new AlarmMsgManager();

        WebServiceHost host = null;
        /// <summary>
        ///  服务启动
        /// </summary>
        public AISmartEventService()
        {
            InitializeComponent();
        }
        /// <summary>
        ///   启动 
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            try
            {
                //人脸服务API
                AIEventManagerServer Services = new AIEventManagerServer();
                var url = System.Configuration.ConfigurationManager.AppSettings["SmartAPIService"];
                host = new WebServiceHost(Services, new Uri(url));
                foreach (System.ServiceModel.Description.ServiceEndpoint EP in host.Description.Endpoints)
                {
                    EP.Behaviors.Add(new BehaviorAttribute());
                }
                host.Open();
               

                AlarmManager.StartServer();
            }
            catch(Exception ex)
            {
                Utility.LogHelper.InstanctLogHelper.WriteLog("StartServer Err:" + ex.Message);
            }
        }
        /// <summary>
        /// 停止
        /// </summary>
        protected override void OnStop()
        {
            try
            {
                if (AlarmManager != null)
                {
                    AlarmManager.isRun = false;
                    AlarmManager.StopServer();
                }
                if (host!=null)
                {
                    host.Close();
                }
            }
            catch (Exception ex) { Utility.LogHelper.InstanctLogHelper.WriteLog("ServerStop Err:" + ex.Message); }
        }
    }
}
